import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/register",
    component: () => import("@/views/register/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        name: "欢迎界面",
        component: () => import("@/views/welcome/index.vue"),
        meta: {
          title: "欢迎界面",
          icon: "form",
        },
      },
    ],
  },
];
export const asyncRoutes = [
  {
    path: "/list",
    component: Layout,
    redirect: "/list/index",
    children: [
      {
        path: "index",
        name: "古树名树信息管理",
        component: () => import("@/views/dashboard/index"),
        meta: {
          title: "古树名树信息管理",
          icon: "form",
          name: "tree.info.list",
        },
      },
    ],
  },
  {
    path: "/example",
    component: Layout,
    redirect: "/example/table",
    name: "疾病伤病信息上报",
    meta: { title: "疾病伤病信息上报", icon: "el-icon-s-help" },
    children: [
      {
        path: "table",
        name: "疾病上报",
        component: () => import("@/views/table/index"),
        meta: { title: "疾病上报", icon: "table", name: "illness.info.list" },
      },
      {
        path: "tree",
        name: "疾病审核",
        component: () => import("@/views/tree/index"),
        meta: { title: "疾病审核", icon: "tree", name: "iTlness.info.audit" },
      },
    ],
  },

  {
    path: "/sortTab",
    component: Layout,
    redirect: "/nested/menu1",
    name: "古树名树分类与统计",
    meta: {
      title: "古树名树分类与统计",
      icon: "nested",
    },
    children: [
      {
        path: "sort",
        component: () => import("@/views/sort/index.vue"), // Parent router-view
        name: "古树名树分类管理",
        meta: { title: "古树名树分类管理", name: "tree.kind.list" },
      },
      {
        path: "tab",
        component: () => import("@/views/tab/index.vue"),
        name: "古树名树统计",
        meta: { title: "古树名树统计", name: "data.statistics.info" },
      },
    ],
  },
  {
    path: "/nested",
    component: Layout,
    redirect: "/nested/menu1",
    name: "古树名树维护与巡查",
    meta: {
      title: "古树名树维护与巡查",
      icon: "nested",
    },
    children: [
      {
        path: "menu1",
        component: () => import("@/views/nested/menu1/index.vue"), // Parent router-view
        name: "古树名树维护记录",
        meta: { title: "古树名树维护记录", name: "maintenance.info.list" },
      },
      {
        path: "publish1",
        component: () => import("@/views/nested/menu1/publish.vue"), // Parent router-view
        name: "维护记录上报",
        meta: { title: "维护记录上报", name: "maintenance.info.publish" },
      },
      {
        path: "menu2",
        component: () => import("@/views/nested/menu2/index"),
        name: "古树名树巡查记录",
        meta: { title: "古树名树巡查记录", name: "inspectionInfo.info.list" },
      },
      {
        path: "publish2",
        component: () => import("@/views/nested/menu2/publish.vue"), // Parent router-view
        name: "巡查记录上报",
        meta: { title: "巡查记录上报", name: "inspectionInfo.info.publish" },
      },
    ],
  },
  {
    path: "/system",
    component: Layout,
    redirect: "/system/user",
    name: "系统设置",
    meta: {
      title: "系统设置",
      icon: "nested",
    },
    children: [
      {
        path: "permission",
        component: () => import("@/views/permission/index.vue"), // Parent router-view
        name: "权限管理",
        meta: { title: "权限管理", name: "permisstion.info.list" },
      },
      {
        path: "user",
        component: () => import("@/views/user/index.vue"), // Parent router-view
        name: "用户管理",
        meta: { title: "用户管理", name: "platform.employee.list" },
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
